#!/usr/bin/env bash

# Copyright 2024 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset
set -o pipefail
set -o xtrace

REPO_ROOT=$(git rev-parse --show-toplevel)
cd ${REPO_ROOT}

if [[ -z "${ARTIFACTS:-}" ]]; then
  echo "ARTIFACTS is not set; skipping dump-artifacts"
  exit 0
fi

mkdir -p ${ARTIFACTS}/networking/
ip route 2>&1 > ${ARTIFACTS}/networking/ip-route || true

ip link 2>&1 > ${ARTIFACTS}/networking/ip-link || true

ip addr 2>&1 > ${ARTIFACTS}/networking/ip-addr || true

sudo iptables --list-rules 2>&1 > ${ARTIFACTS}/networking/iptables-rules || true
sudo iptables -t nat --list-rules 2>&1 > ${ARTIFACTS}/networking/iptables-nat-rules || true

mkdir -p ${ARTIFACTS}/logs/
journalctl --no-pager --user -xeu qemu-dhcp.service 2>&1 > ${ARTIFACTS}/logs/qemu-dhcp.service || true
journalctl --no-pager --user -xeu qemu-storage.service 2>&1 > ${ARTIFACTS}/logs/qemu-storage.service || true
journalctl --no-pager --user -xeu qemu-vm0.service 2>&1 > ${ARTIFACTS}/logs/qemu-vm0.service || true
journalctl --no-pager --user -xeu qemu-vm1.service 2>&1 > ${ARTIFACTS}/logs/qemu-vm1.service || true
journalctl --no-pager --user -xeu qemu-vm2.service 2>&1 > ${ARTIFACTS}/logs/qemu-vm2.service || true

# TODO: Replace this with toolbox dump
mkdir -p ${ARTIFACTS}/vms/
for vm in 0 1 2; do
  ip="10.123.45.1${vm}"
  vm_name="vm${vm}"
  mkdir -p ${ARTIFACTS}/vms/${vm_name}/logs/
  mkdir -p ${ARTIFACTS}/vms/${vm_name}/manifests/
  scp -o StrictHostKeyChecking=accept-new -i ${REPO_ROOT}/.build/.ssh/id_ed25519 root@${ip}:/var/log/etcd* ${ARTIFACTS}/vms/${vm_name}/logs/ || true
  scp -o StrictHostKeyChecking=accept-new -i ${REPO_ROOT}/.build/.ssh/id_ed25519 root@${ip}:/var/log/kube* ${ARTIFACTS}/vms/${vm_name}/logs/ || true
  scp -o StrictHostKeyChecking=accept-new -i ${REPO_ROOT}/.build/.ssh/id_ed25519 root@${ip}:/etc/kubernetes/manifests/* ${ARTIFACTS}/vms/${vm_name}/manifests/ || true

  ssh -o StrictHostKeyChecking=accept-new -i ${REPO_ROOT}/.build/.ssh/id_ed25519 root@${ip} journalctl --no-pager -u kubelet 2>&1 > ${ARTIFACTS}/vms/${vm_name}/logs/kubelet.service || true
  ssh -o StrictHostKeyChecking=accept-new -i ${REPO_ROOT}/.build/.ssh/id_ed25519 root@${ip} journalctl --no-pager -u kops-configuration 2>&1 > ${ARTIFACTS}/vms/${vm_name}/logs/kops-configuration.service || true

  ssh -o StrictHostKeyChecking=accept-new -i ${REPO_ROOT}/.build/.ssh/id_ed25519 root@${ip} ip route 2>&1 > ${ARTIFACTS}/vms/${vm_name}/ip-route || true
  ssh -o StrictHostKeyChecking=accept-new -i ${REPO_ROOT}/.build/.ssh/id_ed25519 root@${ip} ip addr 2>&1 > ${ARTIFACTS}/vms/${vm_name}/ip-addr || true
  ssh -o StrictHostKeyChecking=accept-new -i ${REPO_ROOT}/.build/.ssh/id_ed25519 root@${ip} iptables --list-rules 2>&1 > ${ARTIFACTS}/vms/${vm_name}/iptables-rules || true
  ssh -o StrictHostKeyChecking=accept-new -i ${REPO_ROOT}/.build/.ssh/id_ed25519 root@${ip} iptables -t nat --list-rules 2>&1 > ${ARTIFACTS}/vms/${vm_name}/iptables-nat-rules || true
done

# Dump the pod logs for all the pods in system namespaces
for ns in kube-system; do
  mkdir -p ${ARTIFACTS}/logs/${ns}/
  for pod in $(kubectl get pods -n ${ns} -o json | jq -r '.items[].metadata.name'); do
    kubectl logs -n ${ns} ${pod} > ${ARTIFACTS}/logs/${ns}/${pod}.log || true
  done
done

# Use `kops toolbox dump` to dump a lot of useful information
# We pass --cloud-resources=false because dumping cloud resources is not implemented on metal
mkdir -p ${ARTIFACTS}/dump
${KOPS} toolbox dump \
  --dir ${ARTIFACTS}/dump \
  --k8s-resources \
  --cloud-resources=false \
  --private-key ${REPO_ROOT}/.build/.ssh/id_ed25519 \
  --ssh-user root \
  --name metal.k8s.local
